package com.example.arithmeticleetcode.leetcode.dec;

import java.util.Stack;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-12-31 17:52
 **/
public class Demo31 {

//    ["BrowserHistory","visit","back","back","forward","forward","visit","visit","back"]
//            [["zav.com"],["kni.com"],[7],[7],[5],[1],["pwrrbnw.com"],["mosohif.com"],[9]]

//    [null,null,"zav.com","zav.com","kni.com","kni.com",null,null,"zav.com"]

    public static void main(String[] args) {
        BrowserHistory browserHistory = new BrowserHistory("zav.com");
        browserHistory.visit("kni.com");
        browserHistory.back(7);
        browserHistory.back(7);
        browserHistory.forward(5);
        browserHistory.forward(1);
        browserHistory.visit("pwrrbnw.com");
        browserHistory.visit("mosohif.com");
        browserHistory.back(7);
    }
}
//[null,null,"zav.com","zav.com","kni.com","kni.com",null,null,"zav.com"]
class BrowserHistory {

    private Stack<String> forwardStack = new Stack<>();
    private Stack<String> backStack = new Stack<>();

    public BrowserHistory(String homepage) {
        backStack.push(homepage);
    }

    public void visit(String url) {
        backStack.push(url);
        forwardStack.clear();
    }

    public String back(int steps) {
        int len = backStack.size() > steps ? steps : backStack.size();
        for (int i = 0; i < len; i++) {
            if (backStack.size() == 1) continue;
            forwardStack.push(backStack.pop());
        }
        return backStack.peek();
    }

    public String forward(int steps) {
        int len = forwardStack.size() > steps ? steps : forwardStack.size();

        for (int i = 0; i < len; i++) {
            backStack.push(forwardStack.pop());
        }
        return backStack.peek();
    }
}

